﻿// --------------------------------
// <copyright file="CalculationModelTest.cs" company="TRONSoft">
//     Copyright (c) TRONSoft.
// </copyright>
// <license>
//   This source code is subject to terms and conditions of the Microsoft
//   Public License. A copy of the license can be found in the License.html
//   file at the root of this distribution. If you cannot locate the
//   Microsoft Public License, please send an email to dlr@microsoft.com.
//   By using this source code in any fashion, you are agreeing to be bound 
//   by the terms of the Microsoft Public License. You must not remove this
//   notice, or any other, from this software.
// </license>
// <author>Tron</author>
// ---------------------------------

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tronsoft.Actucal.CalculationEngineCreator.Test
{
    
    
    /// <summary>
    ///This is a test class for CalculationModelTest and is intended
    ///to contain all CalculationModelTest Unit Tests
    ///</summary>
    [TestClass()]
    public class CalculationModelTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Sources
        ///</summary>
        [TestMethod()]
        [DeploymentItem("CalculationEngineCreator.dll")]
        public void Should_Have_Source_Code()
        {
            CalculationParameterCollection collection = new CalculationParameterCollection();
            collection.Add(new CalculationParameter<double>("p1", 1));

            CalculationModel model = new CalculationModel(collection);

            Formula formula = new Formula("Test", "return 1 + p1");

            model.Calculations.Add(new Calculation(formula));

            Assert.IsFalse(model.Source.Length == 0);
        }

        /// <summary>
        ///A test for ReferencedAssemblies
        ///</summary>
        [TestMethod()]
        [DeploymentItem("CalculationEngineCreator.dll")]
        public void The_Result_Should_Be_2()
        {
            CalculationParameterCollection collection = new CalculationParameterCollection();
            collection.Add(new CalculationParameter<double>("p1", 1));

            CalculationModel model = new CalculationModel(collection);

            Formula formula = new Formula("Test", "return 1 + p1");
            model.Calculations.Add(new Calculation(formula));

            Assert.AreEqual(2, model.Eval("Test"));
        }

        /// <summary>
        ///A test for ReferencedAssemblies
        ///</summary>
        [TestMethod()]
        [DeploymentItem("CalculationEngineCreator.dll")]
        public void The_Result_Should_Be_20()
        {
            CalculationParameterCollection collection = new CalculationParameterCollection();
            collection.Add(new CalculationParameter<double>("p1", 1));
            collection.Add(new CalculationParameter<double>("p2", 10));

            CalculationModel model = new CalculationModel(collection);

            Formula formula = new Formula("Test", "return (1 + p1) * p2");
            model.Calculations.Add(new Calculation(formula));

            Assert.AreEqual(20, model.Eval("Test"));
        }

        [TestMethod()]
        public void The_Model_Should_Accept_String_As_Type_Parameter()
        {
            CalculationModel model = new CalculationModel();
            CalculationParameter<string> parameter = model.AddCalculationParameter<string>("foo", "Hello");

            Assert.AreEqual("Hello", parameter.Value);
        }

        [TestMethod()]
        public void The_SystemMath_Functions_Must_Be_Able_To_Be_Used()
        {
            CalculationParameterCollection collection = new CalculationParameterCollection();
            collection.Add(new CalculationParameter<double>("p1", 1));
            collection.Add(new CalculationParameter<double>("p2", 100));

            CalculationModel model = new CalculationModel(collection);

            Formula formula = new Formula("Test", "return (1 + p1) * sqrt(p2)");
            model.Calculations.Add(new Calculation(formula));

            Assert.AreEqual(20, model.Eval("Test"));
        }

        [TestMethod()]
        [DeploymentItem("CalculationEngineCreator.dll")]
        [ExpectedException(typeof(CalculationException), "Unknown calculation")]
        public void Should_Throw_An_Exception_Indicating_That_The_Parameter_Does_Not_Exist()
        {
            CalculationParameterCollection collection = new CalculationParameterCollection();
            collection.Add(new CalculationParameter<double>("p1", 1));

            CalculationModel model = new CalculationModel(collection);

            Formula formula = new Formula("Test", "return 1 + p1");
            model.Calculations.Add(new Calculation(formula));
            model.Eval("p1");
        }

        [TestMethod()]
        [DeploymentItem("CalculationEngineCreator.dll")]
        public void The_Result_Should_Be_20_And_Then_30()
        {
            CalculationParameterCollection collection = new CalculationParameterCollection();
            collection.Add(new CalculationParameter<int>("p1", 1));
            collection.Add(new CalculationParameter<int>("p2", 10));
            collection.Add(new CalculationParameter<int>("p3", 10));

            CalculationModel model = new CalculationModel(collection);

            model.Calculations.Add(new Calculation(new Formula("Test", "return (1 + p1) * p2")));
            model.Calculations.Add(new Calculation(new Formula("Test2", "return p3 + Test")));

            Assert.AreEqual(20, model.Eval("Test"));
            Assert.AreEqual(30, model.Eval("Test2"));
        }

        [TestMethod()]
        public void Should_Save_Assembly()
        {
            CalculationParameterCollection collection = new CalculationParameterCollection();
            collection.Add(new CalculationParameter<int>("p1", 1));
            collection.Add(new CalculationParameter<int>("p2", 10));
            collection.Add(new CalculationParameter<int>("p3", 10));

            CalculationModel model = new CalculationModel(collection);

            model.Calculations.Add(new Calculation(new Formula("Test", "return (1 + p1) * p2")));
            model.Calculations.Add(new Calculation(new Formula("Test2", "return p3 + Test")));

            model.SaveAssembly(@"c:\temp\test_assembly.dll");
        }

        [TestMethod()]
        public void Should_Have_Constants()
        {
            CalculationModel model = new CalculationModel();
            model.Constants.Add(new NameValueCombination<int>("const1", 1));
            model.Constants.Add(new NameValueCombination<DateTime>("const1", DateTime.Now));

            Assert.AreNotEqual(0, model.Constants.Count);
        }

        [TestMethod()]
        public void Should_Generate_Source()
        {
            CalculationParameterCollection collection = new CalculationParameterCollection();
            collection.Add(new CalculationParameter<int>("p1", 1));
            collection.Add(new CalculationParameter<int>("p2", 10));
            collection.Add(new CalculationParameter<int>("p3", 10));

            CalculationModel model = new CalculationModel(collection);
            model.Constants.Add(new NameValueCombination<int>("const1", 1));
            model.Constants.Add(new NameValueCombination<DateTime>("const2", DateTime.Now));
            model.Constants.Add(new NameValueCombination<String>("const3", "Hello world"));
            //model.Constants.Add(new NameValueCombination<DateTime>("const1", DateTime.Now));

            model.Calculations.Add(new Calculation(new Formula("Test", "return (1 + p1) * p2")));
            model.Calculations.Add(new Calculation(new Formula("Test2", "return p3 + Test")));

            SourceCodeGenerator sg = new SourceCodeGenerator(model);
            string retval = sg.GetSourceCode();

            Assert.AreNotEqual(0, retval.Length);
        }

        [TestMethod()]
        public void Add_Constant_Name_Value_Test()
        {
            CalculationModel model = new CalculationModel();
            NameValueCombination<string> actual = model.AddConstant<string>("name", "");
            Assert.AreEqual(model.Constants[0], actual);
        }
        
        [TestMethod()]
        public void Add_Constant_Name_Test()
        {
            CalculationModel model = new CalculationModel(); 
            NameValueCombination<string > actual  = model.AddConstant<string>("name");
            Assert.AreEqual(model.Constants[0], actual);
        }

        /// <summary>
        ///A test for AddCalculationParameter
        ///</summary>
        [TestMethod()]
        public void Add_Calculation_Parameter_Name_Value_Test()
        {
            CalculationModel model = new CalculationModel(); 
            CalculationParameter<double> actual = model.AddCalculationParameter<double>("name", 0F);
            Assert.AreEqual(model.CalculationParameters["name"], actual);
        }

        /// <summary>
        ///A test for AddCalculation
        ///</summary>
        [TestMethod()]
        public void Add_Calculation_Name_Value_Test()
        {
            CalculationModel model = new CalculationModel(); 
            Calculation actual = model.AddCalculation("name", "value");
            Assert.AreEqual(model.Calculations[0], actual);
        }

        /// <summary>
        ///A test for AddCalculation
        ///</summary>
        [TestMethod()]
        public void Add_Calculation_Name_Test()
        {
            CalculationModel model = new CalculationModel();
            Formula formula = new Formula("test", "test");
            Calculation actual = model.AddCalculation(formula);
            Assert.AreEqual(model.Calculations[0], actual); 
        }

        /// <summary>
        ///A test for AddCalculationParameter
        ///</summary>
        [TestMethod()]
        public void Add_Calculation_Parameter_Test()
        {
            CalculationModel model = new CalculationModel(); 
            CalculationParameter<int> actual = model.AddCalculationParameter<int>("p1");
            Assert.AreEqual(model.CalculationParameters["p1"], actual);
        }

        /// <summary>
        ///A test for AddMethod
        ///</summary>
        [TestMethod()]
        public void Add_Method_Test()
        {
            CalculationModel model = new CalculationModel();

            string name = "name";
            MethodSignature signature = new MethodSignature();
            signature.AddParameter("p1", typeof(int));
            signature.ReturnValue = typeof(int);
            string body =
@"int count = 0;
count += 1;
return count;";

            MethodDefinition expected = new MethodDefinition(name, signature, body);
            MethodDefinition actual = model.AddMethod(name, signature, body);

            Assert.AreEqual(expected.Name, actual.Name);
            Assert.AreEqual(expected.Signature, actual.Signature);
            Assert.AreEqual(expected.Body, actual.Body);
        }

        /// <summary>
        ///A test for AddReference
        ///</summary>
        [TestMethod()]
        public void Add_Reference_Test()
        {
            CalculationModel model = new CalculationModel();
            string name = "test.dll";
            model.AddReference(name);
            Assert.IsTrue(model.ReferencedAssemblies.Exists(delegate(string assemblyName){ return (assemblyName == name); })); 
        }

        /// <summary>
        ///A test for AddInclude
        ///</summary>
        [TestMethod()]
        public void Add_Include_Test()
        {
            CalculationModel model = new CalculationModel();
            string test = "test";
            model.AddInclude(test);
            Assert.IsTrue(model.Includes.Exists(delegate(string include) { return (include == test); })); 
        }

        /// <summary>
        ///A test for AddConstant
        ///</summary>
        [TestMethod()]
        public void AddConst_Should_Add_NameValueCombination_As_Constant()
        {
            CalculationModel target = new CalculationModel(); 
            INameValueCombination constant = new NameValueCombination<string>("test", "test"); 
            target.AddConstant(constant);
            Assert.AreEqual(target.Constants[0], constant);
        }

        /// <summary>
        ///A test for AddConstant
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void AddConst_Should_Not_Add_A_Null_NameValueCombination_As_Constant()
        {
            CalculationModel target = new CalculationModel();
            target.AddConstant(null);
        }
    }
}
